//
//  MTLoginViewController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/4.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTLoginViewController.h"
#import "LTcountryCodeSelcectController.h"
#import "MLLinkLabel.h"
#import "MTMainViewController.h"
@interface MTLoginViewController ()

@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet UILabel *contoryCodeLable;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;



@property(nonatomic,strong) MLLinkLabel *protoclLabel;

@property(nonatomic,copy) NSString *contryCode;

@end

@implementation MTLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLable.text = NSLTLocalizedString(@"login.phonelogin", nil);
    self.phoneTextField.placeholder = NSLTLocalizedString(@"register.inputphone", nil);
    self.passwordTextField.placeholder = NSLTLocalizedString(@"register.inputppassworld", nil);
    [self.sureButton setTitle:NSLTLocalizedString(@"main.sure", nil) forState:UIControlStateNormal];
    self.phoneTextField.text = self.phone;
    self.contryCode = @"+86";
    
//    self.protoclLabel = [MLLinkLabel new];
//    self.protoclLabel.textAlignment = NSTextAlignmentRight;
//    NSString* protocolStr = [NSString stringWithFormat:@"%@《%@》",NSLTLocalizedString(@"login.loginMeans", nil), NSLTLocalizedString(@"login.aggree", nil)];
//    NSString* aggreementStr = [NSString stringWithFormat:@"《%@》", NSLTLocalizedString(@"login.aggree", nil)];
//    self.protoclLabel.textColor = [UIColor lightGrayColor];
//    self.protoclLabel.numberOfLines = 2;
//    self.protoclLabel.font = [UIFont systemFontOfSize:12];
//    NSMutableAttributedString* attString= [[NSMutableAttributedString alloc]initWithString:protocolStr];
//    [attString setAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0x94BD31), NSLinkAttributeName : @"asdsd"} range:[protocolStr rangeOfString:aggreementStr]];
//    self.protoclLabel.attributedText = attString;
//    @weakify(self);
//    [self.protoclLabel setDidClickLinkBlock:^(MLLink *link, NSString *linkText, MLLinkLabel *label) {
//        @strongify(self);
//        [self protolClicked:nil];
//    }];
//    [self.view addSubview:self.protoclLabel];
//    
//    [self.protoclLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.sureButton.mas_left).offset(0);
//        make.top.mas_equalTo(self.sureButton.mas_bottom).offset(20);
//        make.right.mas_equalTo(self.sureButton.mas_right).offset(0);
//    }];
    RACSignal *combinSignal = [RACSignal combineLatest:@[self.phoneTextField.rac_textSignal, self.passwordTextField.rac_textSignal] reduce:^id(NSString *account, NSString *pwd){
        if(account.length > 4 && pwd.length > 5)
        {
            return  @(YES);
        }else
        {
            return @(NO);
        }
    }];
    
    [combinSignal subscribeNext:^(id  _Nullable x) {
        BOOL enable = [x boolValue];
        if(enable)
        {
            self.sureButton.backgroundColor = mainButtonColor ;
            [self.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.sureButton.enabled = YES;
        }else
        {
            self.sureButton.backgroundColor = UIColorFromRGB(0xdfdfdf);
            [self.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.sureButton.enabled = NO;
        }
    }];
    
    [[MTVerCodeApiManager manager] getDefaultCountryCodeWithCompletion:^(MMCountry *code, MTError *mterror) {
        if(mterror == nil)
        {
            self.contoryCodeLable.text = code.dialCode;
            self.contryCode = code.dialCode;
        }
    }];
    
    [self.phoneTextField becomeFirstResponder];
}

-(void)protolClicked:(UITapGestureRecognizer *)tap
{
    NSLog(@"protolClicked");
}

- (IBAction)selectCodeClick:(UIButton *)sender {
    
    LTcountryCodeSelcectController *selectCodeVc = [[LTcountryCodeSelcectController alloc]  init];
    selectCodeVc.phoneblock = ^(NSString *name,NSString *code){
        NSLog(@"--%@",code);
        self.contryCode =  code;
        self.contoryCodeLable.text = self.contryCode;
    };
    [self.navigationController pushViewController:selectCodeVc animated:YES];
}

- (IBAction)sureClick:(UIButton *)sender {

    [MBProgressHUD showProcessHud:NSLTLocalizedString(@"login.loading", @"HUD loading title") toView:self.view];
    [[MTUserApiManager manager] loginWithphone:self.phoneTextField.text password:self.passwordTextField.text countryCode:self.contryCode
                                    completion:^(MTUserModel *user, MTError *mterror) {
        [MBProgressHUD hideHUDForView:self.view];
        if(mterror == nil){

            
            [[LTClient sharedClient] loginWithUser:[LTSDKHelper usertransformMTUser:user]];
            MTMainViewController *mainVc = [[MTMainViewController alloc] init];
            [UIApplication sharedApplication].keyWindow.rootViewController = mainVc;
        }else
        {
            [MBProgressHUD showError:mterror.errorDescription];
        }
    }];
}


@end
